package com.hhh.petadopt.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hhh.petadopt.pojo.User;
import com.hhh.petadopt.pojo.UserPage;
import com.hhh.petadopt.pojo.UserVo;
import com.hhh.petadopt.service.AdminUserService;
import com.hhh.petadopt.utils.JsonUtils;
import com.hhh.petadopt.utils.Page;

/**
 * 后台用户管理
 * @author 杜镇声声
 *
 */
@Controller
public class AdminUsercontroller {
	
	@Autowired
	private AdminUserService adminuserservice;
	
	@RequestMapping(value="/adminUser/user.html")
	public String selectAlluser(UserPage vo,Model model) {
		
		//查询用户信息
		Page<User> page = adminuserservice.selectAllUser(vo);
		//将数据存储到内存
		model.addAttribute("total", page.getTotal());
		model.addAttribute("page", page);
		return "admin/user";	
	}
	
	//按条件查询
	@RequestMapping(value="/admin/userlist.html")
	public String searchUser(UserVo vo,Model model) {
		//查询
		Page<User> page = adminuserservice.selectUserByVo(vo);
		//搜索条件回显
		model.addAttribute("username1", vo.getUsername());
		model.addAttribute("address1", vo.getAddress());
		//将总条数存储起来
		model.addAttribute("total", page.getTotal());
		//将当前页信息存储到域中
		model.addAttribute("page", page);
		//返回视图
		return "admin/user";
	}
	
	//编辑页面信息回显
	@RequestMapping(value="/admin/showuser.html")
	@ResponseBody
	public String showUser(Integer uid,HttpServletResponse response) {
		 response.setContentType("text/html;charset=utf-8");// 加上这个处理问号
		//执行查询
		User selectUserById = adminuserservice.selectUserById(uid);
		String objectToJson = JsonUtils.objectToJson(selectUserById);
		return objectToJson;
	}
	//添加用户
	@RequestMapping(value="/adminUser/adduser.html")
	@ResponseBody
	public String adduser(User user) {
		adminuserservice.addusers(user);
		return "ok";
	}
	//删除用户
	@RequestMapping(value="/adminUser/deleteUser.html")
	@ResponseBody
	public String deleteUserByid(Integer uid) {
		//删除
		adminuserservice.deleteUser(uid);
		return "ok";
	}
	//批量删除用户
	@RequestMapping(value="/adminUser/deleteUserss.html")
	@ResponseBody
	public String deleteUserss(Integer[] ids) {
		adminuserservice.deletedUserByIds(ids);
		return "ok";
	}
	
	//修改用户
	@RequestMapping(value="/adminUser/updateUser.html")
	@ResponseBody
	public String updateUser(User user) {
		adminuserservice.updateUserr(user);
		return "ok";
	}

}
